﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AsignChoferAuto
{
    public partial class Listado_ACA_Auto : Form
    {
        private DataTable dt;
        private DateTime fecha;
        private int auto_id;
        private int chofer_id;
        private int turno_id;
        private string auto_text;
        private string chofer_text;
        private string turno_text;

        public Listado_ACA_Auto(DateTime fec, int auto, string auto_txt, int chofer, string chofer_txt, int turno, string turno_txt)
        {
            fecha = fec;
            auto_id = auto;
            chofer_id = chofer;
            turno_id = turno;
            auto_text = auto_txt;
            chofer_text = chofer_txt;
            turno_text = turno_txt;

            InitializeComponent();

            SqlConnection con = Modelo.Modelo.createConnection();
            SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getMarcasTaxi(), con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            busqueda_marca_cb.DataSource = ds.Tables[0].DefaultView;
            busqueda_marca_cb.DisplayMember = "Marca";
            busqueda_marca_cb.ValueMember = "Id";
            busqueda_marca_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con);

            SqlConnection con2 = Modelo.Modelo.createConnection();
            SqlDataAdapter da2 = new SqlDataAdapter(Modelo.SqlQueries.getMarcaModeloReloj(), con2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            busqueda_reloj_cb.DataSource = ds2.Tables[0].DefaultView;
            busqueda_reloj_cb.DisplayMember = "Marca";
            busqueda_reloj_cb.ValueMember = "Id";
            busqueda_reloj_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con2);

            FillData();
        }

        void FillData()
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da;
                dt = new DataTable();
                da = new SqlDataAdapter(Modelo.SqlQueries.getEnabledAutos, con);
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }

        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int columnIndex = e.ColumnIndex;
            DataGridViewRow row = dataGridView1.Rows[rowIndex];
            auto_id = Convert.ToInt32(row.Cells["Id"].Value);
            auto_text = String.Concat(row.Cells["Marca"].Value, " - ", row.Cells["Model"].Value);
            Login.Login.mainForm.openForm(new AsignChoferAuto(fecha, auto_id, auto_text, chofer_id, chofer_text, turno_id, turno_text));
        }

        private void buscar_btn_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                
                SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getAutoFiltered(
                        busqueda_marca_cb.SelectedValue == null ? -1 : Convert.ToInt32(busqueda_marca_cb.SelectedValue),
                        busqueda_modelo_tb.Text,
                        busqueda_patente_tb.Text,
                        busqueda_licencia_tb.Text,
                        busqueda_reloj_cb.SelectedValue == null ? -1 : Convert.ToInt32(busqueda_reloj_cb.SelectedValue)), con);
                dt.Dispose();
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void limpiar_btn_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getEnabledAutos, con);
                dt.Dispose();
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }
    }
}
